<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends MY_Controller {
    
    private $model='category';
    private $table='category';

	function __construct()
    {
        parent::__construct();
		$this->check_manage_session();//判断用户是否登录
    }
	
    public function index()
    {
       
    }
    
	public function channel($en_name)
	{
	    $channel_id = $this->admin_model->get_field("channel","id","en_name='$en_name'");
		$data['channel'] = $this->admin_model->get_show("channel",$channel_id);
		$data['list'] = $this->admin_model->get_list($this->table,"","","channel_id='$channel_id'","root_id ASC,sort ASC,id ASC");
		$data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
		$this->load->view($this->config->item('system_manage_path').'category_list',$data);
	}
		
	public function add($en_name,$parent_id='0')
	{
	    $channel_id = $this->admin_model->get_field("channel","id","en_name='$en_name'");
	    $data['channel'] = $this->admin_model->get_show("channel",$channel_id);
	    $data['channel_id'] = $channel_id;
	    $data['parent_id'] = $parent_id;
	    $data['show'] = $this->admin_model->get_select_max($this->table,'sort',"parent_id=$parent_id");
		$data['list'] = $this->admin_model->get_list($this->table,"","","channel_id='$channel_id'","root_id ASC,sort ASC,id ASC");
		$data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
		$this->load->view($this->config->item('system_manage_path').'category_add',$data);
	}
	
	public function edit($en_name,$id)
	{
	    $channel_id = $this->admin_model->get_field("channel","id","en_name='$en_name'");
	    $data['channel'] = $this->admin_model->get_show("channel",$channel_id);
	    $data['show'] = $this->admin_model->get_show($this->table,$id);
		$data['list'] = $this->admin_model->get_list($this->table,"","","channel_id='$channel_id'","root_id ASC,sort ASC,id ASC");
		$data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
		$this->load->view($this->config->item('system_manage_path').'category_edit',$data);
	}
	
    public function save($en_name,$action,$id=NULL){
		$post=$this->input->post();
        if ($action=='add') {
	        $post['root_id'] = $this->admin_model->get_root_id($post['parent_id']);
	        $post['depth'] = $this->admin_model->get_depth($post['parent_id']);
	    }
		$data=unset_array($post,'lcomeurl'); //过滤无效表单项
		
		$lcomeurl=get_comurl($this->input->post('lcomeurl'));
		
		if($action=='add' or $action=='edit'){
    		if(empty($post['channel_id'])){
    		    $this->error('所属模块不能为空',2);
    		}
    		if(empty($post['name'])){
    			$this->error('请输入分类名称',2);
    		}
    		if(empty($post['sort'])){
    			$this->error('分类排序不能为空',2);
    		}
		}
		
		if($action=='add'){
		    $action_text="添加";
		    $data['createtime'] = time();
			$result=$this->admin_model->add($this->table,$data);
		}elseif($action=='edit'){
		    $action_text="修改";
		    if(empty($id)){
		        $this->error('参数错误',2);
		    }
			$result=$this->admin_model->edit($this->table,$data,$id);
		}elseif($action=='del'){
		    $action_text="删除";
		    if(empty($id)){
		        $this->error('参数错误',2);
		    }
			$result=$this->admin_model->del($this->table,$id);
		}
		if($result){
		    $lcomeurl=str_replace("welcome", "channel", $lcomeurl);
			$this->success($action_text.'成功',2,$lcomeurl);
		}else{
			$this->error($action_text.'失败',2);
		}
	}
	
}
